
package edu.nus.iss.t5.jass.dao;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import edu.nus.iss.t5.jass.entity.Entity;
import edu.nus.iss.t5.jass.entity.JobAdvertisement;
import edu.nus.iss.t5.jass.exception.DAOException;
import edu.nus.iss.t5.jass.exception.DuplicateEntityException;
import edu.nus.iss.t5.jass.exception.InvalidDataException;

import junit.framework.TestCase;



/**
 * Unit Testing Class for AdvertisementDAO
 * 
 * @author Modified by Team
 * 
 */
public class AdvertisementDAOJUnit extends TestCase {
	JobAdvertDAO dao;
		
	public AdvertisementDAOJUnit(String name) {
		super(name);		
		try {
			dao = new JobAdvertDAO();
		} catch (IOException e) {
			e.printStackTrace();

		}
		assertNotNull(dao);
	}

	
	protected void setUp() throws Exception {
		super.setUp();	
		
		Date now = new Date();    
		
		try {
			 
			
			JobAdvertisement ad = new JobAdvertisement("A*STAR", "Abdul Khader Azeem", "Software Engineer ", "Software Deveelopment","Team Leading",(int)1, now, now, now, 3000, 4000, "Singapore", "Senior Manager",2, now,now  );
			dao.save(ad);
		} catch (InvalidDataException e) {
			e.printStackTrace();
			assertFalse("setUp()", true);
		} catch (DuplicateEntityException e) {
			e.printStackTrace();
			assertFalse("setUp()", true);
		} catch (IOException e) {
			e.printStackTrace();
			assertFalse("setUp()", true);
		}
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * Test method for {@link DAO.JobAdvertDAO#update(DTO.EntityDTO)}.
	 */
	public void testUpdate() {
		
		int size = dao.size();
		assertTrue("number of elements in dao is less than 0", size>0);
		
		Entity[] allAds = dao.getAll();	
		JobAdvertisement ad1 = (JobAdvertisement)allAds[0];

		try {
			String newCompany = "NewCompany";
			ad1.setCompany(newCompany);		
			String newOwner = "NewOwner";			
			ad1.setOwner(newOwner);		
			dao.update(ad1);
			
			JobAdvertisement ad2 = (JobAdvertisement) dao.get(ad1.getId());
			assertEquals("id is not equal", ad2.getId(), ad1.getId());
			assertEquals("company is not equal", ad2.getCompany(), newCompany);
			assertEquals("Owner is not equal", ad2.getOwner(), newOwner);									
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertFalse("testUpdate", true);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertFalse("testUpdate", true);
		}
	}

	/**
	 * Test method for {@link DAO.JobAdvertDAO#delete(java.lang.String)}.
	 * @throws DAOException 
	 */
	public void testDelete() throws DAOException {
		
		int size = dao.size();
		assertTrue("number of elements in dao is less than 0", size>0);
		
		Entity[] allAds = dao.getAll();	
		JobAdvertisement ad1 = (JobAdvertisement)allAds[0];

		try {
			dao.remove(ad1);
						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertFalse("testDelete", true);
		}		
			
	}

	/**
	 * Test method for {@link DAO.JobAdvertDAO#get(java.lang.String)}.
	 */
	public void testGet() {
		try {
			Entity[] allAds = dao.getAll();
			
			JobAdvertisement ad1 = (JobAdvertisement)allAds[0];
			
			JobAdvertisement ad = (JobAdvertisement)dao.get(ad1.getId());
			assertEquals("id is not equal", ad1.getId(), ad.getId());			
			assertEquals("Owner is not equal", ad1.getOwner(), ad.getOwner());			
			assertEquals("Title is not equal", ad1.getTitle(), ad.getTitle());			
		} catch (ParseException e) {
			e.printStackTrace();
			assertFalse("testResult", true);
		}	
	}	

	/**
	 * Test method for {@link DAO.JobAdvertDAO#add(DTO.Entity)}.
	 * @throws DAOException 
	 */
	public void testAdd() throws DAOException {
		//first get the size of Advertisement DAO
		int size = dao.size();
		Date now = new Date();    
		JobAdvertisement ad = null;
		try {
			ad = new JobAdvertisement("A*STAR", "Abdul Khader Azeem", "Software Engineer ", "Software Deveelopment","Team Leading",(int)1, now, now, now, 3000, 4000, "Singapore", "Senior Manager",2, now,now  );
			dao.save(ad);
			//System.out.println("Id of new Advertisement is " + ad.getId());
		} catch (InvalidDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertFalse("testToAdd", true);
		} catch (DuplicateEntityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertFalse("testToAdd", true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertFalse("testToAdd", true);
		}		
		
		try {
			JobAdvertisement ad1 = (JobAdvertisement)dao.get(ad.getId());
			assertEquals("id is not equal", ad.getId(), ad1.getId());			
			assertEquals("owner is not equal", ad.getOwner(), ad1.getOwner());
			assertEquals("title is not equal", ad.getTitle(), ad1.getTitle());			
		} catch (ParseException e) {
			e.printStackTrace();
			assertFalse("testToAdd", true);
		}	
	}

	/**
	 * Test method for {@link DAO.JobAdvertDAO#JobAdvertDAO()}.
	 */
	public void testJobAdvertDAO() {		
		//fail("Not yet implemented");
	}

}
